package com.rk.pethome.user.controller;
import com.alibaba.fastjson.JSONObject;
import com.rk.pethome.basic.util.AjaxResult;
import com.rk.pethome.basic.util.RedisUtils;
import com.rk.pethome.user.Service.IUserService;
import com.rk.pethome.user.Vo.UserDot;
import com.rk.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;


    /**
     * 通过邮箱进行注册
     * @param userDot
     * @return
     */
    @PostMapping("/registerByEmial")
    public AjaxResult registerByEmial(@RequestBody UserDot userDot){
        try {
            System.out.println(userDot);
            return userService.registerEmail(userDot);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败");
        }
    }



    /**
     * 校验邮箱是否存在
     * @param param
     * @return
     */
    @PostMapping("/checkEmail")
    public AjaxResult checkEmail(@RequestBody Map<String,Object> param){
        try{
            //获取邮箱
            String email = (String) param.get("email");
            return userService.checkEmail(email);
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }


    /**
     * 校验手机号码是否存在
     * @param param
     * @return
     */
    @PostMapping("/checkPhone")
    public AjaxResult checkPhone(@RequestBody Map<String,Object> param){
        try{
            //获取手机号码
            String phone = (String) param.get("phone");
            userService.checkPhone(phone);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }


    /**
     * 发送手机验证码
     * @param param
     * @return
     * 发送验证码
     */
    @PostMapping("/sendMobileCode")
    public AjaxResult sendMobileCode(@RequestBody Map<String,String> param) {
        try {
            //获取前端传递的手机验证码
            String phone = param.get("phone");
            //发送
            userService.sendMobileCodebyPhone(phone);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }


    /**
     * 通过手机号注册
     * @param userDot
     * @return
     */
    @PostMapping("/registerByPhone")
    public AjaxResult register(@RequestBody UserDot userDot){
        try {
            System.out.println(userDot);
            return userService.registerPhone(userDot);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败");
        }
    }


    /**
     * 通过邮箱或者手机号登陆
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        try {
            //通过用户名和密码查询并进行校验
            AjaxResult ajaresult = userService.login(user);
            //校验通过
            if(ajaresult.getSuccess()) {
                //通过用户名查找user
                User loginuser = userService.loadByUsername(user.getUsername());
                //随机产生redis的key值
                String token = UUID.randomUUID().toString();
                //将loginuser转化为json字符串 存入redis
                RedisUtils.INSTANCE.set(token, JSONObject.toJSONString(loginuser),30*60);
                Map<String,Object> map=new HashMap<>();
                //把token存储起来
                map.put("token",token);
                //把对象存储起来
                map.put("loginUser",loginuser);
                return new AjaxResult().setResult(map);
            }
            //为通过校验直接返回
            return ajaresult;
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 通过邮箱或者手机号登陆
     * @return
     */
    @PostMapping("/userAction")
    public AjaxResult activation(@RequestBody HashMap<String,String> para){
        try {
            System.out.println(para.get("code"));
            String email=para.get("email");
            String code = para.get("code");
            return userService.activation(email,code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
